package pl.javarsovia.javarsovia4android.activities;

import pl.javarsovia.R;
import pl.javarsovia.javarsovia4android.adapters.LectureAdapter;
import pl.javarsovia.javarsovia4android.domain.ConferenceProvider;
import pl.javarsovia.javarsovia4android.domain.Lecture;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SessionsActivity extends Activity implements OnGestureListener, OnTouchListener {

	private static final int LIMIT = 30;

	private GestureDetector gestureScanner;
	private boolean moved = false;
	private LectureAdapter lectureAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sessions_list);
		initListView();
		gestureScanner = new GestureDetector(this);
		Toast.makeText(this, getResources().getString(R.string.sessions_info), Toast.LENGTH_LONG).show();
	}

	private void initListView() {
		lectureAdapter = new LectureAdapter(this, R.layout.sessions_row,
				ConferenceProvider.getConference());
		getListView().setAdapter(lectureAdapter);
		getListView().setOnTouchListener(this);
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Lecture lecture = (Lecture) getListView().getItemAtPosition(position);
				if (lecture.getSpeaker() != null) {
					SessionsActivity activity = SessionsActivity.this;
					Intent intent = new Intent(activity, LectureDetailsActivity.class);
					intent.putExtra(LectureDetailsActivity.LECTURE, lecture);
					activity.startActivity(intent);
				}
			}
		});
	}

	public ListView getListView() {
		return (ListView) findViewById(R.id.sessions_list);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return true;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float x, float y) {
		if (x > LIMIT) {
			lectureAdapter.nextSession();
			moved = true;
		} else if (x < -LIMIT) {
			lectureAdapter.prevSession();
			moved = true;
		}
		return true;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (moved == false) {
			return super.dispatchTouchEvent(ev);
		} else {
			moved = false;
			ev.setAction(MotionEvent.ACTION_CANCEL);
			super.dispatchTouchEvent(ev);
			return true;
		}
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return true;

	}

	@Override
	public boolean onTouch(View arg0, MotionEvent event) {
		getListView().onTouchEvent(event);
		return gestureScanner.onTouchEvent(event);
	}
}
